package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template1.DestinationTemplate;
import cn.wolfcode.luowowo.search.template1.StrategyTemplate;
import cn.wolfcode.luowowo.search.template1.TravelTemplate;
import cn.wolfcode.luowowo.search.template1.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Administrator on 2019/7/16.
 */
@Controller
public class SearchController {
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("/q")
    public String query(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        String urlStr = "";
        switch (qo.getType()) {
            case 0:
                urlStr = "searchDest";
                destSearch(model, qo);
                break;
            case 1:
                urlStr = "searchStrategy";
               strategySearch(model, qo);
                break;
            case 2:
                urlStr = "searchTravel";
                travelSearch(model, qo);
                break;
            case 3:
                urlStr = "searchUser";
                userSearch(model, qo);
                break;
            default:
                urlStr = "searchAll";
                allSearch(model, qo);
        }
        return "index/" + urlStr;
    }

    private void userSearch(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> userInfoTemplates = searchService.searchWithHighlight(
                UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class,
                qo, "nickname", "destName"
        );
        model.addAttribute("page",userInfoTemplates);
    }

    private void travelSearch(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> travelTemplates = searchService.searchWithHighlight(
                TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME,
                TravelTemplate.class,
                qo, "title", "summary"
        );
        model.addAttribute("page",travelTemplates);
    }

    private void strategySearch(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> strategyTemplates = searchService.searchWithHighlight(
                StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class,
                qo, "title", "subTitle", "summary"
        );
        model.addAttribute("page",strategyTemplates);
    }

    private void allSearch(Model model, SearchQueryObject qo) {
        //目的地
        Page<DestinationTemplate> destinationTemplates = searchService.searchWithHighlight(
                DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class,
                qo, "name", "info"
        );
        //攻略
        Page<StrategyTemplate> strategyTemplates = searchService.searchWithHighlight(
                StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class,
                qo, "title", "subTitle", "summary"
        );
        //游记
        Page<TravelTemplate> travelTemplates = searchService.searchWithHighlight(
                TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME,
                TravelTemplate.class,
                qo, "title", "summary"
        );
        //用户
        Page<UserInfoTemplate> userInfoTemplates = searchService.searchWithHighlight(
                UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class,
                qo, "nickname", "destName"
        );
        SearchResultVO vo = new SearchResultVO();
        vo.setStrategys(strategyTemplates.getContent());
        vo.setTravels(travelTemplates.getContent());
        vo.setUsers(userInfoTemplates.getContent());
        vo.setDests(destinationTemplates.getContent());
        long total = travelTemplates.getTotalElements()+strategyTemplates.getTotalElements()
                +userInfoTemplates.getTotalElements()+destinationTemplates.getTotalElements();
        vo.setTotal(total);
        model.addAttribute("data",vo);
    }

    private void destSearch(Model model, SearchQueryObject qo) {
        //1.根据关键字查询目的地对象
        DestinationTemplate dest = destinationSearchService.queryDestByName(qo.getKeyword());
        qo.setPageSize(5);
        qo.setDestId(dest.getId());
        qo.setDestName(dest.getName());
        if (dest!=null){
            Page<TravelTemplate> travelTemplates = travelSearchService.query(qo);
            Page<StrategyTemplate> strategyTemplates = strategySearchService.queryByDestId(qo);
            Page<UserInfoTemplate> userInfoTemplates = userInfoSearchService.query(qo);
            //data
            SearchResultVO vo = new SearchResultVO();
            vo.setStrategys(strategyTemplates.getContent());
            vo.setTravels(travelTemplates.getContent());
            vo.setUsers(userInfoTemplates.getContent());
            long total = travelTemplates.getTotalElements()+strategyTemplates.getTotalElements()
                    +userInfoTemplates.getTotalElements();
            vo.setTotal(total);
            model.addAttribute("data",vo);
        }
        //dest
        model.addAttribute("dest",dest);
    }
}
